package com.ettractions.kiosk.mediators
{
	import com.ettractions.kiosk.Kiosk;
	import com.ettractions.kiosk.components.ScreenSaver;
	import com.ettractions.kiosk.events.ThumbnailGridEvent;
	import com.ettractions.kiosk.events.ViewStateEvent;
	import com.greensock.TweenLite;
	
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.core.IVisualElement;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.DataGroup;
	
	public class ScreenSaverMediator extends Mediator
	{
		public static const TWEEN_DURATION:uint = 2;
		
		[Inject]
		public var view:ScreenSaver;
		
		private var _magnifyingGlassTween:TweenLite;
		
		private var _appWidth:uint;
		
		private var _appHeight:uint;
		
		private var _currentItemIndex:int = -1;
		
		override public function onRegister():void
		{
			_appWidth = view.parentApplication.width;
			_appHeight = view.parentApplication.height;
			
			view.visible = false;
			
			eventDispatcher.addEventListener(ViewStateEvent.IDLING, handleIdling);
		}
		
		private function moveMagnifyingGlass(resetBeforeTween:Boolean = false):void
		{
			var group:DataGroup = Kiosk(view.parentApplication).thumbnailGrid.page.list.dataGroup;
			var index:uint = Math.floor(group.numElements * Math.random());
			var element:IVisualElement;
			var coords:Point;
			
			while (index == _currentItemIndex)
				index = Math.floor(group.numElements * Math.random());
			
			_currentItemIndex = index;
			
			if (resetBeforeTween)
			{
				element = group.getElementAt(0);
				
				coords = group.localToGlobal(new Point(element.x + (element.width / 2), element.y + (element.height / 2)));
				
				view.graphic.x = coords.x;
				view.graphic.y = coords.y;
			}
			
			element = group.getElementAt(index);
			coords = group.localToGlobal(new Point(element.x + (element.width / 2), element.y + (element.height / 2)));
			
			_magnifyingGlassTween = new TweenLite(
				view.graphic,
				TWEEN_DURATION,
				{
					x:coords.x,
					y:coords.y,
					onUpdate:handleTweenUpdate,
					onComplete:handleTweenComplete
				}
			);
			
			_magnifyingGlassTween.play();
		}
		
		private function handleIdling(event:ViewStateEvent):void
		{
			eventDispatcher.addEventListener(ViewStateEvent.USER_INTERACTION, handleUserInteraction);
			eventDispatcher.addEventListener(ThumbnailGridEvent.GRID_PAGE_CHANGING, handleGridPageChanging);
			eventDispatcher.addEventListener(ThumbnailGridEvent.GRID_PAGE_CHANGED, handleGridPageChanged);			
		}
		
		private function handleUserInteraction(event:ViewStateEvent):void
		{
			view.visible = false;
			
			eventDispatcher.removeEventListener(ViewStateEvent.USER_INTERACTION, handleUserInteraction);
			eventDispatcher.removeEventListener(ThumbnailGridEvent.GRID_PAGE_CHANGING, handleGridPageChanging);
			eventDispatcher.removeEventListener(ThumbnailGridEvent.GRID_PAGE_CHANGED, handleGridPageChanged);						
		}
		
		private function handleGridPageChanging(event:ThumbnailGridEvent):void
		{
			view.visible = false;
		}
		
		private function handleGridPageChanged(event:ThumbnailGridEvent):void
		{
			var bitmapData:BitmapData = new BitmapData(_appWidth, _appHeight, false);
			
			bitmapData.draw(view.root, null, null, null, new Rectangle(0, 0, _appWidth, _appHeight), true);
			
			view.image.source = bitmapData;
			
			view.visible = true;
			
			moveMagnifyingGlass();
		}		
		
		private function handleTweenUpdate():void
		{
			view.image.x = -(((_appWidth * view.image.scaleX) - _appWidth) * (view.graphic.x / _appWidth));
			view.image.y = -(((_appHeight * view.image.scaleY) - _appHeight) * (view.graphic.y / _appHeight));
		}
		
		private function handleTweenComplete():void
		{
			if (view.visible)
				moveMagnifyingGlass();
			
			else
				_magnifyingGlassTween = null;
		}
	}
}